<template>
  <el-dialog title="创建菜单" :visible="addVisible" @close="close" width="500px" @open="open">
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item label="权限组名称" label-width="100px">
        <el-radio v-model="form.is_point" label="0">菜单</el-radio>
        <el-radio v-model="form.is_point" label="1">权限点</el-radio>
      </el-form-item>
      <el-form-item label="权限组名称" label-width="100px">
        <!-- <el-select v-if="form.isPoint === 0" ref="departmentname" v-model="newpid" placeholder="请选择">
          <el-option v-loading="loading" value="" style="height: 200px">
            <el-tree
              :default-expand-all="true"
              :data="datas"
              :props="defaultProps"
              @node-click="nodeClick"
            />
          </el-option>
        </el-select> -->
        <el-select  ref="departmentname" v-model="newpid" placeholder="请选择">
          <el-option v-loading="loading" value="" style="height: 200px">
            <el-tree
            node-key="id"
              :default-expand-all="true"
              :data="disabelData"
              :props="defaultProps"
              @node-click="nodeClick"
            />
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="权限代码" label-width="100px" prop="code">
        <el-input style="width:250px;" v-model="form.code" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="权限标题" label-width="100px" prop="title">
        <el-input style="width:250px;" v-model="form.title" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, detail, update } from '@/api/base/menus.js'
export default {
  name: 'MenusAdd',
  props: {
    addVisible: {
      type: Boolean,
      required: true
    }
  },
  data () {
    return {
      detailId: '',
      form: {
        id: '',
        is_point: '0',
        code: '',
        title: '',
        pid: ''
      },
      newpid: '',
      loading: false,
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      datas: [],
      disabelData: [],
      rules: {
        code: [{ required: true, message: '请输入权限代码' }],
        title: [{ required: true, message: '请输入权限标题' }]
      }
    }
  },
  methods: {
    close () {
      this.form = {
        is_point: '0',
        code: '',
        title: '',
        pid: ''
      }
      this.detailId = ''
      this.newpid = ''
      this.$refs.form.resetFields()
      this.$emit('update:addVisible', false)
    },
    nodeClick (row) {
      this.$refs.departmentname.blur()
      this.form.pid = row.pid
      this.newpid = row.title
    },
    async open () {
      this.disabelData.forEach(item => item)
      if (this.detailId) {
        const { data } = await detail({ id: this.detailId })
        this.form = data
        this.newpid = data.title
        if (data.is_point) {
          this.form.is_point = '1'
        } else {
          this.form.is_point = '0'
        }
      }
    },
    async onSubmit () {
      const point = this.form.is_point !== '0'
      if (this.detailId) {
        await update({ ...this.form, is_point: point })
        this.$message.success('编辑成功')
      } else {
        await add({ ...this.form, is_point: point })
        this.$message.success('添加成功')
      }
      this.close()
      this.$emit('updateMenus')
    }
  },
  // watch: {
  //   form: {
  //     deep: true,
  //     immediate: true,
  //     handler (newvalue) {
  //       console.log(newvalue)
  //       this.newpid = this.form.title
  //     }
  //   }
  // }
}
</script>

<style lang="less" scoped>
  .el-select-dropdown__item.hover,
  .el-select-dropdown__item:hover .el-select-dropdown__item {
    background-color: #fff;
    overflow: unset;
  }
</style>
